<?php
include_once("ThePage.class.php");
$page = new ThePage();
if($page->check_login()){
	jumpPage('index.php');
}else{
	if($_POST){
		$user_id = reg_user();
		if($user_id>0){
			$page->userRequest->set('u_id',$user_id);
		}elseif($user_id==0){
			alertMsg('-5000');
		}elseif($user_id==-2){
			alertMsg('-5001');
		}else{
			alertMsg('-2001');
		}
	}else{
		$page->smarty->display('reg.tpl');
	}
}

function reg_user(){
	$user_name = isset($_POST['user_name']) ? $_POST['user_name'] : '';
	$user_password = isset($_POST['user_password']) ? md5($_POST['user_password']) : '';
	$user_mail = isset($_POST['user_mail']) ? $_POST['user_mail'] : '';
	$user_sex = isset($_POST['user_sex']) ? $_POST['user_sex'] : 0;
	$time = time();
	if($user_name && $user_password){
		$user_array = array('user_name'=>$user_name,
						'user_password'=>$user_password,
						'user_mail'=>$user_mail,
						'user_sex'=>$user_sex,
						'user_state'=>1,
						'user_ugtime'=>$time
						);
		$user_db = Factory::getDB('demo_user');
		$where = "user_name = '{$user_name}' or user_mail = '{$user_mail}'";
		$rowNo = $user_db->getSumNumber($user_db->tableName,$where);
		if($rowNo>1){
			return -2;
		}
		$user_id = $user_db->insert_demo_user($user_array);
		if($user_id>0){
			//注册SESSION登陆
			//$this->userRequest->set('u_id',$user_id);
			return $user_id;
		}else{
			return -1;
		}
	}else{
		return 0;
	}	
}
?>